﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain.Logic3D
{
    public class CameraBob
    {
        public float FootStepSize = 1;
        public float BobSize = 1;
        public ICamera3D Camera;
        public int Frames = 5;
        public int TimeMS;

        public CameraBob(float footStepSize, float bobSize, int frames, int timeToUnBob, ICamera3D camera)
        {
            this.FootStepSize = footStepSize;
            this.BobSize = bobSize;
            this.Camera = camera;
            this.Frames = frames;
            this.TimeMS = timeToUnBob;
        }

        private Vector3 lastPosition;
        private float walked;
        private bool bobbing = false;
        private int timer;
        private float Y;
        public void Update(Vector3 currentPosition)
        {
            float dif = Vector3.Distance(currentPosition, lastPosition);
            lastPosition = currentPosition;

            if (dif > 0.5f)
            {
                if (bobbing)
                {
                    timer += Engine.Instance.GameTime.ElapsedGameTime.Milliseconds;
                    if (timer < TimeMS)
                    {
                        Y = AnimationUtil.SmoothAnimate(Y,
                            BobSize, Frames);
                        Camera.Position = new Vector3(Camera.Position.X, AnimationUtil.SmoothAnimate(Camera.Position.Y,
                            Camera.Position.Y + Y, Frames), Camera.Position.Z);
                    }
                    if (timer > TimeMS)
                    {
                        Y = AnimationUtil.SmoothAnimate(Y,
                        0, Frames);
                        Camera.Position = new Vector3(Camera.Position.X, AnimationUtil.SmoothAnimate(Camera.Position.Y,
                            Camera.Position.Y + Y, Frames), Camera.Position.Z);

                        if (timer > (int)(TimeMS * 2))
                        {
                            timer = 0;
                            bobbing = false;
                            walked = 0;
                            Y = 0;
                        }
                    }
                }
                else
                {
                    walked += dif;
                    if (walked > FootStepSize)
                    {
                        // Bob camera
                        bobbing = true;
                        timer = 0;
                        walked = 0;
                    }
                }
            }
            else
            {
                timer = 0;
                bobbing = false;
                walked = 0;
                Y = 0;
            }
        }
    }
}
